package com.tmc.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.common.Pagination;
import com.tmc.entity.common.EthInterfaceAccount;
import com.tmc.util.ObjectUtil;

@Repository
public class EthInterfaceAccountDaoImpl extends BaseDaoImpl<EthInterfaceAccount> {

	public Pagination<EthInterfaceAccount> findByCon(ContextInfo context, EthInterfaceAccount cond, int pageIndex, int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from EthIntefaceAccount c where c.enabledFlag = 'Y' ");
		if (ObjectUtil.hasText(cond.getVendorCode())) {
			sql.append(" and (c.vendorCode = :vendorCode or c.vendorName like :vendorCode) ");
			params.put("vendorCode", "%" + cond.getVendorCode() + "%");
		}
		sql.append(" order by c.lastUpdateDate desc ");
		return this.findPagination(sql.toString(), params, pageIndex, pageSize);
	}

	public List<EthInterfaceAccount> findAll(ContextInfo context) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from EthIntefaceAccount c where c.enabledFlag = 'Y' ");
		return this.findList(sql.toString(), params);
	}

	public EthInterfaceAccount getByAccountKey(ContextInfo context, String vendorCode, String accountType, String environment) {
		if (ObjectUtil.noText(vendorCode) || ObjectUtil.noText(accountType)) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from EthIntefaceAccount c where c.enabledFlag = 'Y' ");
		sql.append(" and c.vendorCode = :vendorCode ");
		sql.append(" and c.accountType = :accountType ");
		sql.append(" and c.environment = :environment ");
		params.put("vendorCode", vendorCode);
		params.put("accountType", accountType);
		params.put("environment", environment);
		return this.get(sql.toString(), params);
	}
	
	public EthInterfaceAccount getByName(ContextInfo context, String name) {
		if (!ObjectUtil.hasText(name)) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from EthIntefaceAccount c where c.enabledFlag = 'Y' ");
		sql.append(" and c.vendorName = :name ");
		params.put("name", name);
		return this.get(sql.toString(), params);
	}

}
